﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCSY.BLL;
using WCSY.MODEL;

public partial class UserManagement_MainRoleRight : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){

            string roleId = Request["roleId"];
            //string addFlg = Request["addrr"];
            //if (addFlg != null && !addFlg.Equals("") && Convert.ToInt32(addFlg) == 1)
            //{   
            //    //根据角色id删除角色权限表内容
            //    Mg_rrRelationManager.DeleteMg_rrRelationById(roleId);
            //    for (int i = 0; i < roleRightTreeView.Nodes.Count;i++ )
            //    {
            //        if(roleRightTreeView.Nodes[i].Checked==true){
            //        string rightsId = roleRightTreeView.Nodes[i].Value;

            //        Mg_rrRelation rr = new Mg_rrRelation();

            //        rr.RoleId = roleId;

            //        rr.RightsId = Convert.ToInt32(rightsId);

            //        Mg_rrRelationManager.AddMg_rrRelation(rr);
            //        }

            //    }
                
            //}
            roleRightTreeView.Attributes.Add("OnClick", "goPostBack()"); 
            initTree(roleId);
           
        }
    }

    private void initTree(string roleId) {

        string sql = "select * from mg_rights where parentid = 0";
        List<Mg_right> rightList = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;
       
        roleRightTreeView.Nodes.Clear();
        foreach(Mg_right right in rightList){
            TreeNode root = new TreeNode();
            root.Value = right.Rightsid.ToString();
            //System.Diagnostics.Debug.WriteLine(root.Value);
            root.Text = right.Rightsname;
            this.roleRightTreeView.Nodes.Add(root);
            initChildTree(root,roleId);
        }
    
    
    }

    private void initChildTree(TreeNode child,string roleId) {
        string sql = "select * from mg_rights where parentid = " + Convert.ToInt32(child.Value) +"";
        
        List<Mg_right> rightList = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;
        List<Mg_rrRelation> rrList = null;
        if (roleId != null && !roleId.Equals(""))
        {
            string rrsql = "select * from mg_rrrelation where roleId = " + roleId + "";
            //Response.Write("<script>alert('"+roleId+"')</script>");
            rrList = Mg_rrRelationManager.GetMg_rrRelationBysql(rrsql) as List<Mg_rrRelation>;
        }

        foreach(Mg_right childRight in rightList){
            
            TreeNode childNode = new TreeNode();
            childNode.Value = childRight.Rightsid.ToString();
            childNode.Text = childRight.Rightsname;

            child.ChildNodes.Add(childNode);
            if (rrList != null)
            {
                foreach (Mg_rrRelation rr in rrList)
                {
                    //System.Diagnostics.Debug.WriteLine("rightsid:"+rr.RightsId+"childnode:"+child.Value);
                    if (rr.RightsId==Convert.ToInt32(childNode.Value))
                    {   
                        childNode.Checked = true;
                        childNode.Parent.Checked = true;
                       // Response.Write("<script>alert('" + roleId + "')</script>");

                    }

                }
            }

            initChildTree(childNode,roleId);

                }
    }

    private bool updateTreeNode = false; 
    protected void roleRightTreeView_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        if (updateTreeNode) return;   //   如果在刷新中 
        updateTreeNode = true;
        try
        {
            TreeNodeChild(e.Node, e.Node.Checked);   //   同步子节点 
            TreeNodeCheck(e.Node, e.Node.Checked);
            addRoleRight();
           
        }
        finally
        {
            updateTreeNode = false;
        } 
    }



    public void addroleRightsChild(TreeNode tn, string roleId)
    {
        if (tn.Checked == true)
        {
            Mg_rrRelation rr = new Mg_rrRelation();
            rr.RoleId = roleId;
            rr.RightsId = Convert.ToInt32(tn.Value);
            Mg_rrRelationManager.AddMg_rrRelation(rr);

        }

        foreach (TreeNode tnc in tn.ChildNodes)
        {

            addroleRightsChild(tnc, roleId);

        }
    }

    private void addRoleRight()
    {
        //根据角色id删除角色权限表内容
        string roleId = Request["roleId"];
        if (roleId != null && !roleId.Equals(""))
        {
            Mg_rrRelationManager.DeleteMg_rrRelationById(roleId);
            for (int i = 0; i < roleRightTreeView.Nodes.Count; i++)
            {
                //Response.Write("<script>alert('" + roleRightTreeView.Nodes[i].Checked + "');</script>");
                if(roleRightTreeView.Nodes[i].Checked==true){
                    addroleRightsChild(roleRightTreeView.Nodes[i],roleId);

                    //string rightsId = roleRightTreeView.Nodes[i].Value;

                    //Mg_rrRelation rr = new Mg_rrRelation();

                    //rr.RoleId = roleId;

                    //rr.RightsId = Convert.ToInt32(rightsId);

                    //Mg_rrRelationManager.AddMg_rrRelation(rr);
                }
                

            }

        }
        else
        {
            Response.Write("<script>alert('角色未选择，请选择角色！')</script>");

        }
    }


    private   void   TreeNodeChild(TreeNode   ATreeNode,   bool   AChecked)   //   同步子节点 
    { 
             if   (ATreeNode   ==   null)   return; 
             ATreeNode.Checked   =   AChecked; 
             foreach   (TreeNode   vTreeNode   in   ATreeNode.ChildNodes) 
                         TreeNodeChild(vTreeNode,   AChecked); 
    } 
  
    private   void   TreeNodeCheck(TreeNode   ATreeNode,   bool   AChecked) 
    { 
             if   (ATreeNode   ==   null)   return; 
             ATreeNode.Checked   =   AChecked; 
             if   (AChecked)   //   如果选中本节点这就是选中全部上级节点 
             { 
                         TreeNodeCheck(ATreeNode.Parent,   AChecked); 
             } 
             else 
             { 
                         if   (ATreeNode.Parent   !=   null   &&   ATreeNode.Parent.Checked) 
                         { 
                                     foreach   (TreeNode   vTreeNode   in   ATreeNode.Parent.ChildNodes) 
                                                 if   (vTreeNode.Checked)   return; 
                                     TreeNodeCheck(ATreeNode.Parent,   AChecked);   //   判断是否所有的兄弟节点Checked都为false 
                         } 
             } 
    } 
  

    }
